package com.caishi.lkx;

import cn.hutool.crypto.GlobalBouncyCastleProvider;
import com.caishi.lkx.common.role.Roles;
import com.zzw.common.annotation.ApiLogIgnore;
import com.zzw.common.annotation.ResultIgnore;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationFailedEvent;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;

/**
 * Hello world!
 * mvn package -pl com.vip.langrun:vers7-server -am --also-make -Dmaven.test.skip=true -DskipTests=true
 * vip-vrs7
 * <pre>{@code
 *  --add-opens java.base/java.lang=ALL-UNNAMED
 *  --add-opens java.base/java.io=ALL-UNNAMED
 *  --add-opens java.base/java.math=ALL-UNNAMED
 *  --add-opens java.base/java.net=ALL-UNNAMED
 *  --add-opens java.base/java.nio=ALL-UNNAMED
 *  --add-opens java.base/java.security=ALL-UNNAMED
 *  --add-opens java.base/java.text=ALL-UNNAMED
 *  --add-opens java.base/java.time=ALL-UNNAMED
 *  --add-opens java.base/java.util=ALL-UNNAMED
 *  --add-opens java.base/jdk.internal.access=ALL-UNNAMED
 *  --add-opens java.base/jdk.internal.misc=ALL-UNNAMED
 *  --add-opens java.base/java.lang.reflect=ALL-UNNAMED
 *  }<pre>
 */
@SpringBootApplication
@EnableTransactionManagement
@MapperScan( value = {"com.caishi.lkx.*.mapper","com.zzw.common.service.mapper"})
@EnableConfigurationProperties
@ConfigurationPropertiesScan
@ComponentScan(basePackages={
        "com.zzw.common",
//        "com.caishi.lkx",
        "com.caishi.lkx.common",
        "com.caishi.lkx.exam",
        "com.caishi.lkx.order",
        "com.caishi.lkx.user",
        "com.caishi.lkx.course",
        "com.caishi.lkx.distribution",
        "com.caishi.lkx.utils",
        "com.caishi.lkx.config",
        "com.caishi.lkx.handler",
        "com.caishi.lkx.aop",
})
@EnableCaching
@RestController
@EnableScheduling
public class QuestionServerStart {

    @Value("${keray.schedule.open}")
    private boolean scheduleOpen;
    public static void main(String[] args) {
        GlobalBouncyCastleProvider.setUseBouncyCastle(false);
        System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", "200");
        System.setProperty("java.util.Arrays.useLegacyMergeSort", "true");
        System.setProperty("SYS_POOL_SIZE", "500");
        SpringApplication.run(QuestionServerStart.class, args);
    }

    @PostConstruct
    public void init() {
        System.out.println("keray.schedule.open = " + scheduleOpen);
    }
    @EventListener
    public void started(ApplicationEvent applicationEvent) {
        if (applicationEvent instanceof ApplicationStartedEvent) {
            System.out.println("question-server-start-success");
        } else if (applicationEvent instanceof ApplicationFailedEvent) {
            System.out.println("question-server-start-fail");
        } else if (applicationEvent instanceof ContextClosedEvent) {
            System.out.println("question-server-close");
        }
    }

    @GetMapping("/check-health")
    @ResultIgnore
    @ApiLogIgnore
    @Roles
    public String checkHealth() {
        return "ok";
    }


}
